﻿using System;
using System.Data;
using System.Windows.Forms;
using AMS.Inventory.PDA.Castle.DataAccess;
using AMS.Inventory.PDA.Core.DataAccess;
using System.Collections;
namespace AMS.Inventory.PDA
{
    public partial class FrmCheckInfoList : Form
    {
        private readonly SqliteDBServer _sqldb = null;
        private bool _isFinishBound ; //记录盘点计划是否绑定完成
        private DataRow currentRow;
        private string _frameSql ="";
        private Hashtable _hashtable;
        private string _planId;
        public FrmCheckInfoList()
        {
            InitializeComponent();
        }

        public FrmCheckInfoList(string username, SqliteDBServer sqliteDbServer,string planId)
        {
            InitializeComponent();
            this.txtUserName.Text = username;
            _sqldb = sqliteDbServer;
            _planId = planId;
        }

        private void BtnCloseClick(object sender, EventArgs e)
        {
            Close();
        }

        private void FrmCheckInfoList_Load(object sender, EventArgs e)
        {
            _hashtable = clsLocalization.ReadContectResource(this.Name, ModCommon.DefaultLanguage);
            clsLocalization.getControlNames(this); 
            _frameSql = clsLocalization.getFromContentNames(_hashtable, "_frameSql");
            string checkPlanSql = "";
            DataTable checkPlanDt = null;
            checkPlanSql = "SELECT Id,Name FROM AmsInventory_PlanInfo";
            checkPlanDt = _sqldb.GetDataTable(checkPlanSql);
            if (checkPlanDt != null && checkPlanDt.Rows.Count > 0)
            {
                _isFinishBound = false;
                cboCheckPlan.DataSource = checkPlanDt;
                cboCheckPlan.  ValueMember = checkPlanDt.Columns["Id"].ToString();
                cboCheckPlan.DisplayMember = checkPlanDt.Columns["Name"].ToString();
                _isFinishBound = true;
                this.cboCheckPlan.SelectedValue=_planId;
            }

        }

        private void CboCheckPlanSelectedIndexChanged(object sender, EventArgs e)
        {
            string planSql = "";
            //if (_isFinishBound)
            //{
            //    string planId = "";
            //    DataTable assetsDt = null;
            //    if (cboCheckPlan.Text != "")
            //        planId = cboCheckPlan.SelectedValue.ToString();
            //    if (planId != "")
            //    {
            //        dgrdAssetsInfo.DataSql = _frameSql + " where PlanId='" + planId + "'";
            //        dgrdAssetsInfo.CountSql= "SELECT count(*) FROM AmsInventory_InventoryInfo where PlanId='" + planId + "'";
            //        dgrdAssetsInfo.FrameSql = this._frameSql;
            //        dgrdAssetsInfo.ReFlashGrid();
            //        txtUserName.Text = "";
            //    }
            //}
        }

        private string PlanId
        {
            get { return cboCheckPlan.SelectedValue.ToString(); }
        }

        private void BtnCheckingClick(object sender, EventArgs e)
        {
            string checker = "";
            if (txtUserName.Text == "")
            {
                MessageBox.Show(clsLocalization.getFromContentNames(_hashtable, "EmptyUser"));
                return;
            }
            checker = txtUserName.Text.Trim();
            var frmCheckInfo = new FrmCheckInfo(checker, _sqldb, PlanId);
            frmCheckInfo.ShowDialog();
            btnSearch_Click(sender,e);

        }

        private void BtnUpdateClick(object sender, EventArgs e)
        {
            if (this.currentRow==null)
            {
                MessageBox.Show(clsLocalization.getFromContentNames(_hashtable, "ChooseAssetsMsg"));
                return;
            }
            DataTable assetsInfoDt = null;
            DataRow assetsRow = null;
            assetsRow = currentRow;
            string barcodeStr = clsLocalization.getFromContentNames(_hashtable, "BarcodeStr");
            string barcode = assetsRow[barcodeStr].ToString();
            string checker = "";
            if (txtUserName.Text == "")
            {
                MessageBox.Show(clsLocalization.getFromContentNames(_hashtable, "EmptyUser"));
                return;
            }
            checker = txtUserName.Text.Trim();
            var frmCheckInfo = new FrmCheckInfo(checker, _sqldb, PlanId, barcode);
            frmCheckInfo.ShowDialog();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string barcodeStr, planId = "";
            if (cboCheckPlan.Text != "")
               planId = cboCheckPlan.SelectedValue.ToString();
            barcodeStr = txtBarcode.Text.Trim();
            if (barcodeStr != null)
            {
                this.dgrdAssetsInfo.DataSql =_frameSql+ "  where  PlanId='" + planId + "' and barcode like '%" + barcodeStr + "%' order by barcode ";
                this.dgrdAssetsInfo.CountSql = "SELECT count(*) FROM AmsInventory_InventoryInfo where  PlanId='" + planId + "'and barcode like '%" + barcodeStr + "%'";
                this.dgrdAssetsInfo.FrameSql = _frameSql;
                this.dgrdAssetsInfo.ReFlashGrid();
                if(this.dgrdAssetsInfo.PageCount<1)
                    this.currentRow = null;

            }
            else
            {
                MessageBox.Show(clsLocalization.getFromContentNames(_hashtable, "InputMsg"));
                return;
            }
        }

        private void dgrdAssetsInfo_CellChanged(object sender, GridClickEventArgs e)
        {
            this.currentRow = e.Row;
        }

    }
}